package com.sudao.android.ball;

import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;

import java.lang.Integer;

public class SoundControl extends PreferenceActivity 
		implements SharedPreferences.OnSharedPreferenceChangeListener {
	
	private static final int SLOW = 100;
	private static final int MEDIUM = 200;
	private static final int FAST = 300;
    
	public static final String PREF_PAD_SPEED = "pad_speed";
	public static final String PREF_SOUND = "sound";
	public static final String PREF_SOUND_VOLUME = "sound_volume";

	public static final int DEFAULT_PREF_PAD_SPEED = MEDIUM;
	public static final boolean DEFAULT_PREF_SOUND = true;
	public static final int DEFAULT_PREF_SOUND_VOLUME = 50;
	
	private static final String TITLE_SLOW = "Slow";
	private static final String TITLE_MEDIUM = "Medium";
	private static final String TITLE_FAST = "Fast";

	private ListPreference padSpeed;
	private CheckBoxPreference sound;
	private SeekBarPreference soundVolume;

	private Intent mIntent = new Intent();
	private Bundle bundle = new Bundle();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		
		String[] titleList = {TITLE_SLOW, TITLE_MEDIUM, TITLE_FAST};
        String[] valueList = {
			Integer.toString(SLOW),
            Integer.toString(MEDIUM),
            Integer.toString(FAST)
		};

        ListPreference padSpeedPref = (ListPreference) findPreference(PREF_PAD_SPEED);
        padSpeedPref.setEntries(titleList);
       	padSpeedPref.setEntryValues(valueList);
		String speed = PreferenceManager.getDefaultSharedPreferences(this).
			getString(PREF_PAD_SPEED, DEFAULT_PREF_PAD_SPEED + "");
        padSpeedPref.setValue(speed);
        padSpeedPref.setSummary(padSpeedPref.getEntry());
		bundle.putInt(PREF_PAD_SPEED, Integer.parseInt(speed));

        CheckBoxPreference soundPref = ((CheckBoxPreference) findPreference(PREF_SOUND));
        soundPref.setChecked(PreferenceManager.getDefaultSharedPreferences(this)
			.getBoolean(PREF_SOUND, DEFAULT_PREF_SOUND));
		bundle.putBoolean(PREF_SOUND, soundPref.isChecked());

        final SeekBarPreference soundVolumePref = (SeekBarPreference) findPreference(PREF_SOUND_VOLUME);
        soundVolumePref.setEnabled(soundPref.isChecked());
		soundVolumePref.setMin(0);
		soundVolumePref.setMax(100);
		soundVolumePref.setListener(new SeekBarPreference.Callback(){
		    public int onSetInitialProgress() {
		    	int volume = PreferenceManager.getDefaultSharedPreferences(SoundControl.this).
		    		getInt(PREF_SOUND_VOLUME, DEFAULT_PREF_SOUND_VOLUME);
				String prefix = getResources().getString(R.string.sound_volume_summary);
			    soundVolumePref.setSummary(prefix + volume);
				return volume;
		    }

		    public String onSetMessage() {
		    	return SoundControl.this.getResources().getString(R.string.sound_volume_message);
		    }

			public void onSaveProgress(int value){
				PreferenceManager.getDefaultSharedPreferences(SoundControl.this).edit().
					putInt(PREF_SOUND_VOLUME, value).commit();
		    }
		});

		int volume = PreferenceManager.getDefaultSharedPreferences(this).
			getInt(PREF_SOUND_VOLUME, DEFAULT_PREF_SOUND_VOLUME);
		bundle.putFloat(PREF_SOUND_VOLUME, volume / 100.0f);

		String prefix = getResources().getString(R.string.sound_volume_summary);
		soundVolumePref.setSummary(prefix + volume);
		
		PreferenceManager.getDefaultSharedPreferences(this)
			.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onDestroy() {
		PreferenceManager.getDefaultSharedPreferences(this)
			.unregisterOnSharedPreferenceChangeListener(this);
		super.onDestroy();
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 
			Preference preference) {
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}
    
	public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
		if (PREF_PAD_SPEED.equals(key)){
			int v = Integer.parseInt(preferences.getString(PREF_PAD_SPEED, DEFAULT_PREF_PAD_SPEED + ""));
			bundle.putInt(key, v);
        	ListPreference padSpeedPref = (ListPreference) findPreference(PREF_PAD_SPEED);
        	padSpeedPref.setSummary(padSpeedPref.getEntry());
		} else if(PREF_SOUND.equals(key)){
			bundle.putBoolean(key, preferences.getBoolean(PREF_SOUND, DEFAULT_PREF_SOUND));
		} else if(PREF_SOUND_VOLUME.equals(key)){
			// fix me: use 100.0 because setMax(100)
        	SeekBarPreference soundVolumePref = (SeekBarPreference) findPreference(PREF_SOUND_VOLUME);
			int volume = preferences.getInt(PREF_SOUND_VOLUME, DEFAULT_PREF_SOUND_VOLUME);
			String prefix = getResources().getString(R.string.sound_volume_summary);
			soundVolumePref.setSummary(prefix + volume);
			bundle.putFloat(key, volume / 100.0f);
		}

		mIntent.putExtras(bundle);
		setResult(RESULT_OK, mIntent);
	}
}
